package com.ruoyi.house.crawling;
import java.util.Date;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.house.crawling.domain.CrawlingAnjukeRecord;
import com.ruoyi.house.crawling.domain.ZuInfo;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CrawlingAnjukeHouseInfo {


    //爬取前的配置
    private static String city = "成都";
//    private static String city = "遂宁";

//    private static String city = "重庆";

//    public static String URL1 = "https://wx.zu.anjuke.com/fangyuan/p{pageStart}/";
    public static String URL1 = "https://cd.zu.anjuke.com/fangyuan/p{pageStart}/";
//    public static String URL1 = "https://sn.zu.anjuke.com/fangyuan/p{pageStart}/";
//    public static String URL1 = "https://cq.zu.anjuke.com/fangyuan/p{pageStart}/";

//    public static String cookie = "id58=CgAEH2Zy5mQ1M1imDncvAg==; ctid=25; lps=https%3A%2F%2Fwx.zu.anjuke.com%2F%7C; cmctid=93; aQQ_ajkguid=9D54BE1D-CE46-54A0-7C14-04DFE44F7C21; id58=CrIej2Zy5m1T6i14DiLGAg==; wmda_uuid=424035307dc5779e9095637c052c925a; wmda_new_uuid=1; wmda_visited_projects=%3B6289197098934; xxzlclientid=264f4914-12df-4ad2-bbf1-1718806126671; xxzlxxid=pfmxyoS5AgZwOLLzM88IKq68jounjtEJJVEYiiLNuwbSuMaRuSRNDg5RDyElBtKNqP5V; sessid=A7109809-7B6A-46C7-8618-AC230464F567; ajk-appVersion=; fzq_h=cbc4d60f0a251f8ba96555b6e5e8b202_1718806170453_4bec29a84004433e9f4fe07da772e5af_47924972912633199377229117823878543207; obtain_by=2; twe=2; 58tj_uuid=6d269246-7d4c-488b-8cba-c2b019ab2290; new_uv=1; xxzlbbid=pfmbM3wxMDMyNXwxLjkuMHwxNzE4ODEyMTcwNzMzMTQ1MTE2fDhxNHNBOWFFWW95ZlNjU1h3RVpnWEJBZFVPY0FvaEE2RWtrdmV4TWc3aFE9fGM4MGU5NzgwYTY2YWZkZDcwNjIyY2E4OGNjMDJlZDJlXzE3MTg4MTIxNjk2MzNfMzA5Y2QwYTU1ZTgwNGUwNzgyOTZjMWMwYTc4MTYyNGJfMjg3NDk0NzkyNnw5NjM5Nzc3NzVkNTQ2YTUyYjE3YzQ5ZGVjNDlmNDhmYl8xNzE4ODEyMTcwMDAxXzI1NA==; xxzl_cid=e936f7096bde4d4393de9c40e3d4759a; xxzl_deviceid=8O1BROp63GisCLfAW3e1vZeGH0MKn5Z5UMSPT6HYPUpUcuLrDCXsPJL61BaM06Ac";
    public static String cookie = "SECKEY_ABVK=swUJv3XI+c+XtQVup0uyL3zqKkB1A8xXj7b67kFAyt4L4815PJig229loLsDOm4IUjFHnZlcYUm/Hro287feGA%3D%3D; BMAP_SECKEY=swUJv3XI-c-XtQVup0uyL3zqKkB1A8xXj7b67kFAyt50apsnWfl562E2Go5BBLKnaJkuTMFY5ry4_f6ALdNIUeEl-Kdjr4-nslaVzJygjqzkPnqow_7AIgDZF9d-3EVsnjlKhAk47qDoasCX2aJ8aO6DG365jWz0_X22JakaSU5va-0-jwPEfrYGQ6uWRQMUObcVR1CcT5_pnb0GfUx-x6gpv4iK11ULZe-jf9BRL3HCsyXMUUaaKoieS3rhhPBb; id58=CgAEH2Zy5mQ1M1imDncvAg==; lps=https%3A%2F%2Fwx.zu.anjuke.com%2F%7C; aQQ_ajkguid=9D54BE1D-CE46-54A0-7C14-04DFE44F7C21; id58=CrIej2Zy5m1T6i14DiLGAg==; xxzlclientid=264f4914-12df-4ad2-bbf1-1718806126671; xxzlxxid=pfmxyoS5AgZwOLLzM88IKq68jounjtEJJVEYiiLNuwbSuMaRuSRNDg5RDyElBtKNqP5V; sessid=A7109809-7B6A-46C7-8618-AC230464F567; ajk-appVersion=; fzq_h=cbc4d60f0a251f8ba96555b6e5e8b202_1718806170453_4bec29a84004433e9f4fe07da772e5af_47924972912633199377229117823878543207; obtain_by=2; twe=2; 58tj_uuid=6d269246-7d4c-488b-8cba-c2b019ab2290; new_uv=1; wmda_visited_projects=%3B6289197098934; wmda_uuid=174ba6924d23b9c2db0062a448e94b8b; wmda_new_uuid=1; ctid=15; cmctid=102; wmda_session_id_6289197098934=1718901896651-716a5b60-2cf7-a0c9; xxzlbbid=pfmbM3wxMDMyNXwxLjkuMXwxNzE4OTAyNzM5MjYxMTA1NTU3fFh5dkcyNFhLQzZ1ZzVFc0NSMFpuenpPSFZBVEpXZkZpY3U1NS9ZSUU3MUE9fGUyNTk4NTg3MTNlOWQ0ZTdlYzcyNjA2OGQ1ZmMxZTU4XzE3MTg5MDI3NDAwNzJfZDc1MTVmNmRiNDE2NDc0NjgzOTk2NWNiNTllZDZmNGVfMTk5NjgxNTU5NnxmOWNjY2ExZGQyYmMxYTVhOGNiNTg4OWQ0MTc0NjVlMV8xNzE4OTAyNzM4MjA5XzI1NA==; xxzl_cid=14240662e5ad4c298bb660b54c03cbe4; xxzl_deviceid=FUdQG/PjKp2pjDPL2V25mt0K6i+DlwI3lV7LWTpzKi8P6kj3WYnDsFn0D0VV54o7";
//    public static String cookie = "id58=CgAEH2Zy5mQ1M1imDncvAg==; lps=https%3A%2F%2Fwx.zu.anjuke.com%2F%7C; aQQ_ajkguid=9D54BE1D-CE46-54A0-7C14-04DFE44F7C21; id58=CrIej2Zy5m1T6i14DiLGAg==; xxzlclientid=264f4914-12df-4ad2-bbf1-1718806126671; xxzlxxid=pfmxyoS5AgZwOLLzM88IKq68jounjtEJJVEYiiLNuwbSuMaRuSRNDg5RDyElBtKNqP5V; sessid=A7109809-7B6A-46C7-8618-AC230464F567; ajk-appVersion=; obtain_by=2; twe=2; 58tj_uuid=6d269246-7d4c-488b-8cba-c2b019ab2290; new_uv=1; fzq_h=afcbaab13f8e69b2e41a8a9925334a6c_1718951464920_af0920bac3af444e90400772496bdb53_47924972912630620073611501382060926517; ctid=215; cmctid=9688; wmda_uuid=621d1b0333874b4a603aec1a52b5edc0; wmda_new_uuid=1; wmda_session_id_6289197098934=1718951490867-848b861a-444a-1284; wmda_visited_projects=%3B6289197098934; xxzlbbid=pfmbM3wxMDMyNXwxLjkuMXwxNzE4OTUxNTc5NzEwNTUyMzkyfElVcGpIckJLRUVvWW52SFZBUXoxZ2M1UENxdmV4dzZreE5zU1F1RFVQQzA9fDA1NzYzZDZlMWMzODJiYmQzZjI0ODBiZTA1YTI0YTRlXzE3MTg5NTE1NzkzODFfMmYxYzkwMzcwMWMwNDg2NDgzN2VlMzA5ZjI5ZTViNTBfMzA2MjU5MjY5M3wzMmYxNWM4ZGFlOWI3NDZmZWE3Y2UxOGVlODljMTc1Zl8xNzE4OTUxNTc4OTI0XzI1NQ==; xxzl_cid=b2b500077ba64f2b9296debac6c96bc3; xxzl_deviceid=Y19lnHnTRju92ZjTcG8H5AcRacdEm9GKQvezatGAJEukpWvpO2bAL4l2ilsWPwop";
//    public static String cookie = "id58=CgAEH2Zy5mQ1M1imDncvAg==; lps=https%3A%2F%2Fwx.zu.anjuke.com%2F%7C; aQQ_ajkguid=9D54BE1D-CE46-54A0-7C14-04DFE44F7C21; id58=CrIej2Zy5m1T6i14DiLGAg==; xxzlclientid=264f4914-12df-4ad2-bbf1-1718806126671; xxzlxxid=pfmxyoS5AgZwOLLzM88IKq68jounjtEJJVEYiiLNuwbSuMaRuSRNDg5RDyElBtKNqP5V; sessid=A7109809-7B6A-46C7-8618-AC230464F567; ajk-appVersion=; obtain_by=2; twe=2; 58tj_uuid=6d269246-7d4c-488b-8cba-c2b019ab2290; new_uv=1; fzq_h=afcbaab13f8e69b2e41a8a9925334a6c_1718951464920_af0920bac3af444e90400772496bdb53_47924972912630620073611501382060926517; ctid=215; cmctid=9688; xxzlbbid=pfmbM3wxMDMyNXwxLjkuMXwxNzE4OTUxNjExMDMzODU3MjU2fE1wUnU0bm9FbjBFTXMydUtqY0RwRHAwcWpQdjB2TWs0RFRvM013MC9sZ2c9fDBjZjIxN2M1NmU5YmNmYmQwZjEzNGRlOGY0YjMzOThkXzE3MTg5NTE2MDk5MDdfNmNmZDYyYmRmZjM4NGIwZGFkMGNhNGRjNzliMzgyMTZfMzA2MjU5MjY5M3wzMDczYjBhNjE1YzhlOTM0YWYyNTkzOGJjZWY4ZWY0YV8xNzE4OTUxNjA5ODM3XzI1NQ==";

    private static String agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36";


    //数据库配置
    private static String driverName = "com.mysql.cj.jdbc.Driver";
    private static String mysqlUrl = "jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static String username = "root";
    private static String password = "qq6031675";

    private static Connection connectionInstance = null;

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException, InterruptedException {
        //System.getProperties().setProperty("proxySet", "true");
        //System.getProperties().setProperty("http.proxyHost","125.87.92.163");
        //System.getProperties().setProperty("http.proxyPort", "4256");

        Long pageStrat = 8L;
        while (true) {
            String url = URL1.replace("{pageStart}", pageStrat + "");
            System.out.println("主页面url: " + url);
            System.out.println("爬取第 "+ pageStrat +" 页");


            // 使用 HttpClient 发送请求
            CloseableHttpClient httpClient = HttpClients.createDefault();

            try {
                HttpGet httpGet = new HttpGet(url);


//                httpGet.setHeader(":authority","wx.zu.anjuke.com");
//                httpGet.setHeader(":method","GET");
//                httpGet.setHeader(":path","/fangyuan/p2/");
//                httpGet.setHeader(":scheme","https");
//                httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
//                httpGet.setHeader(":Accept-Encoding","gzip, deflate, br, zstd");
//                httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.9");
//                httpGet.setHeader("Cache-Control","no-cache");
//                httpGet.setHeader("Pragma","no-cache");
//                httpGet.setHeader("Priority","u=0, i");
//                httpGet.setHeader("Sec-Ch-Ua","\"Google Chrome\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"");
//                httpGet.setHeader("Sec-Ch-Ua-Mobile","?0");
//                httpGet.setHeader("Sec-Ch-Ua-Platform","\"Windows\"");
//                httpGet.setHeader("Sec-Fetch-Dest","document");
//                httpGet.setHeader("Sec-Fetch-Mode","navigate");
//                httpGet.setHeader("Sec-Fetch-Site","none");
//                httpGet.setHeader("Sec-Fetch-User","?1");
//                httpGet.setHeader("Upgrade-Insecure-Requests","1");

                httpGet.setHeader("Cookie",cookie);
                httpGet.setHeader("User-Agent",agent);
                CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

                // 打印响应内容
                String responseBody = EntityUtils.toString(httpResponse.getEntity());
//                System.out.println(responseBody);
                if(StringUtils.isNotNull(responseBody)){

                    //保存爬虫爬取记录
                    CrawlingAnjukeRecord crawlingAnjukeRecord = new CrawlingAnjukeRecord();
                    crawlingAnjukeRecord.setUseCookie(cookie);
                    crawlingAnjukeRecord.setUseUseragent(agent);
                    crawlingAnjukeRecord.setCrawlingCity(city);
                    crawlingAnjukeRecord.setPrimaryPageUrl(url);
                    crawlingAnjukeRecord.setPageNumber(pageStrat);
                    crawlingAnjukeRecord.setCrawlingTime(new Date());
                    crawlingAnjukeRecord.setCreateBy("admin");
                    saveCrawlingRecord(crawlingAnjukeRecord);
                }

                // 关闭响应
                httpResponse.close();


                String returnStr = responseBody;
                // System.out.println(returnStr);
//                Pattern p = Pattern.compile("<a data-company=\"\" class=\"img\" _soj=\"([^\"]*)\" data-sign=\"true\"\r\n"
//                        + " href=\"([^\"]*)\" target=\"_blank\" hidefocus=\"true\">");

//                System.out.println("returnStr = " + returnStr);
                Pattern p = Pattern.compile("<a data-company=\"\" class=\"img\" _soj=\"([^\\\"]*)\" data-sign=\"true\"\n" +
                        "                       href=\"([^\\\"]*)\" target=\"_blank\" hidefocus=\"true\">");

                Matcher m = p.matcher(returnStr);
                while (m.find()) {
                    //System.out.println("帖子详情链接：" + m.group(2));
                    Random rand = new Random();
                    int a = rand.nextInt(10) + 5;
                    Thread.sleep(a * 1000);
                    try {
                        String tempUrlStr = m.group(2);
                        System.out.println("当前链接：" + tempUrlStr);

                        URL tempUrl = new URL(tempUrlStr);
                        HttpURLConnection tempConnection = (HttpURLConnection) tempUrl.openConnection();
                        //设置请求方式
                        tempConnection.setRequestMethod("GET");
                        // 10秒超时
                        tempConnection.setConnectTimeout(10000);
                        tempConnection.setReadTimeout(10000);

//                        tempConnection.setRequestProperty("Cookie", "cookie值");
//                        tempConnection.setRequestProperty("User-Agent", "useragent值");

                        tempConnection.setRequestProperty("Cookie", cookie);
                        tempConnection.setRequestProperty("User-Agent", agent);

                        //连接
                        tempConnection.connect();
                        //得到响应码
                        int tempResponseCode = tempConnection.getResponseCode();
                        System.out.println(tempResponseCode);
                        if (tempResponseCode == HttpURLConnection.HTTP_OK) {
                            //得到响应流
                            InputStream tempInputStream = tempConnection.getInputStream();
                            //获取响应
                            BufferedReader tempReader = new BufferedReader(new InputStreamReader(tempInputStream, "UTF-8"));
                            String tempReturnStr = "";
                            String tempLine;
                            while ((tempLine = tempReader.readLine()) != null) {
                                tempReturnStr += tempLine + "\r\n";
                            }
//                            Pattern p2 = Pattern.compile("<span id=\"houseCode\">房屋编码：([^\\\"]*)，</span>发布时间：<b class=\"strongbox\" style=\"font-weight: normal;\">([^\\\"]*)</b>\r\n");
//                            Matcher m2 = p2.matcher(tempReturnStr);
//                            Pattern p3 = Pattern.compile("<span class=\"type\">面积：</span>\r\n"
//                                    + "        <span class=\"info\"><b class=\"strongbox\" style=\"font-weight: normal;\">([^\"]*)</b></span>");
//                            Matcher m3 = p3.matcher(tempReturnStr);
//                            Pattern p4 = Pattern.compile("<span class=\"type\">朝向：</span>\r\n"
//                                    + "        <span class=\"info\">([^\"]*)</span>");
//                            Matcher m4 = p4.matcher(tempReturnStr);
//                            Pattern p5 = Pattern.compile("<span class=\"type\">楼层：</span>\r\n"
//                                    + "        <span class=\"info\">([^\"]*)</span>");
//                            Matcher m5 = p5.matcher(tempReturnStr);
//                            Pattern p6 = Pattern.compile("<span class=\"type\">小区：</span>\r\n"
//                                    + "        <a href=\"([^\"]*)\" class=\"link\"  target=\"_blank\" _soj=\"propview\">([^\"]*)</a>");
//                            Matcher m6 = p6.matcher(tempReturnStr);
//                            Pattern p7 = Pattern.compile("<span class=\"price\"><em><b class=\"strongbox\" style=\"font-weight: normal;\">([^\"]*)</b></em>元/月</span>");
//                            Matcher m7 = p7.matcher(tempReturnStr);

                            Pattern p2 = Pattern.compile("<span id=\"houseCode\">房屋编码：(.*?)，</span>更新时间：<b class=\"strongbox\" style=\"font-weight: normal;\">(.*?)</b>");
                            Matcher m2 = p2.matcher(tempReturnStr);
                            Pattern p3 = Pattern.compile("        <span class=\"type\">面积：</span>\r\n" +
                                    "        <span class=\"info\"><b class=\"strongbox\" style=\"font-weight: normal;\">(.*?)</b></span>");
                            Matcher m3 = p3.matcher(tempReturnStr);
                            Pattern p4 = Pattern.compile("<span class=\"type\">朝向：</span>\r\n"
                                    + "        <span class=\"info\">(.*?)</span>");
                            Matcher m4 = p4.matcher(tempReturnStr);
                            Pattern p5 = Pattern.compile("<span class=\"type\">楼层：</span>\r\n"
                                    + "        <span class=\"info\">(.*?)</span>");
                            Matcher m5 = p5.matcher(tempReturnStr);
                            Pattern p6 = Pattern.compile("<span class=\"type\">小区：</span>\r\n"
                                    + "        <a href=\"(.*?)\" class=\"link\"  target=\"_blank\" _soj=\"propview\">(.*?)</a>");
                            Matcher m6 = p6.matcher(tempReturnStr);
                            Pattern p7 = Pattern.compile("<span class=\"price\"><em><b class=\"strongbox\" style=\"font-weight: normal;\">(.*?)</b></em>元/月</span>");
                            Matcher m7 = p7.matcher(tempReturnStr);

                            Pattern p8 = Pattern.compile("<span class=\"type\">户型：</span>\r\n" +
                                    "        <span class=\"info\">(.*?)</span>");
                            Matcher m8 = p8.matcher(tempReturnStr);

                            Pattern p9 = Pattern.compile("<h1 class=\"house-title\">\r\n" +
                                    "    <div class=\"strongbox tit-rest\">(.*?)</div>");
                            Matcher m9 = p9.matcher(tempReturnStr);

//                            Pattern p10 = Pattern.compile("<h2 class=\"title\">房源概况</h2>\r\n" +
//                                    "</div>\r\n" +
//                                    "<div class=\"auto-general\">\r\n" +
//                                    "(.*?)\r\n" +
//                                    "</div>");
                            Pattern p10 = Pattern.compile("    <h2 class=\"title\">房源概况</h2>\r\n" +
                                    "</div>\r\n" +
                                    "<div class=\"auto-general\">\r\n" +
                                    "([\\s\\S.]*?)\r\n" +
                                    "</div>\r\n");
                            Matcher m10 = p10.matcher(tempReturnStr);

                            Pattern p11 = Pattern.compile("<span class=\"type\">装修：</span>\r\n" +
                                    "        <span class=\"info\">(.*?)</span>");
                            Matcher m11 = p11.matcher(tempReturnStr);

                            Pattern p12 = Pattern.compile("<div class=\"p_1180 p_crumbs\">\r\n" +
                                    "        <a href=\"(.*?)\">(.*?)</a>");
                            Matcher m12 = p12.matcher(tempReturnStr);

                            Pattern p13 = Pattern.compile("<span class=\"type\">小区：</span>\r\n" +
                                    "([\\s\\S.]*?)<a href=\"(.*?)\" class=\"link\"  target=\"_blank\" _soj=\"propview\">(.*?)</a>\r\n" +
                                    "([\\s\\S.]*?)&nbsp;\\(<a href=\"(.*?)\" class=\"link\" target=\"_blank\" _soj=\"propview\">(.*?)</a> <a href=\"(.*?)\" class=\"link\" target=\"_blank\" _soj=\"propview\">(.*?)</a>\\)\r\n" );
//                                    "([\\s\\S.]*?)</li>");
                            Matcher m13 = p13.matcher(tempReturnStr);

                            //室内图
                            Pattern p14 = Pattern.compile("                <div class=\"switch_list\" id=\"room_pic_wrap\">([\\s\\S.]*?)</div>\r\n" +
                                    "                                                            </div>");
                            Matcher m14 = p14.matcher(tempReturnStr);

                            while (m2.find() && m3.find() && m4.find() && m5.find() && m6.find() && m7.find()
                            && m8.find() && m9.find() && m10.find() && m11.find() && m12.find() && m13.find() && m14.find() ) {
//                                String str = "insert into 安居客(housenumber,time,area,orientation,floor,price,address) values('" +
//                                        m2.group(1) + "','" + m2.group(2) + "','" + m3.group(1) + "','" +
//                                        m4.group(1) + "','" + m5.group(1) + "','" + m7.group(1) + "','" + m6.group(2) + "')";
//                                System.out.println("-----------str = " + str);



                                System.out.println("-----------房屋编码 = " + m2.group(1));
                                System.out.println("-----------发布日期 = " + m2.group(2));
                                System.out.println("-----------面积 = " + m3.group(1));
                                System.out.println("-----------朝向 = " + m4.group(1));
                                System.out.println("-----------楼层 = " + m5.group(1));
                                System.out.println("-----------租金 = " + m7.group(1));
//                                System.out.println("-----------租金 = " + m6.group(2));


                                System.out.println("-----------户型 = " + m8.group(1).replace("<b class=\"strongbox\" style=\"font-weight: normal;\">","")
                                        .replace("</b>",""));
                                System.out.println("-----------标题 = " + m9.group(1));
                                System.out.println("-----------房源概况 = " + m10.group(1));
                                System.out.println("-----------装修 = " + m11.group(1));
                                System.out.println("-----------市 = " + m12.group(2).replace("安居客",""));
                                System.out.println("-----------区 = " + m13.group(6));
                                System.out.println("-----------街道 = " + m13.group(8));
                                System.out.println("-----------小区地址 = " + m6.group(2));

                                String imgHtmlStr = m14.group(1);
//                                System.out.println("-----------图片html片段 = " + imgHtmlStr);
                                String imgSrcs = "";
                                if(imgHtmlStr != null && !imgHtmlStr.equals("")){
                                    Pattern p15 = Pattern.compile("<img data-src=\"(.*?)\"");
                                    Matcher m15 = p15.matcher(imgHtmlStr);

                                    int count = 0;
                                    while (m15.find()){
                                        if(count == 0){
                                            imgSrcs += m15.group(1);
                                            count ++;
                                            continue;
                                        }
                                        imgSrcs += "、"+m15.group(1);
                                        count ++;
//                                        for (int i = 0; i < m15.groupCount(); i++) {
//                                            imgSrcs += "、"+m15.group(i+1);
//                                        }
                                    }
                                }
                                System.out.println("-----------图片 = " + imgSrcs);
                                System.out.println("-----------地址 = " + m12.group(2).replace("安居客","")+"-"+m13.group(6)+"-"+m13.group(8)+"-"+ m6.group(2));

//		                            stat.executeUpdate(str);

                                //保存爬取的数据至房源信息表
                                ZuInfo zuInfo = new ZuInfo();
                                zuInfo.setHouseNumber(m2.group(1));
                                zuInfo.setPushTime(m2.group(2));
                                zuInfo.setArea(m3.group(1));
                                zuInfo.setOrientation(m4.group(1));
                                zuInfo.setFloor(m5.group(1));
                                zuInfo.setPrice(m7.group(1));
                                zuInfo.setHouseType(m8.group(1).replace("<b class=\"strongbox\" style=\"font-weight: normal;\">","")
                                        .replace("</b>",""));
                                zuInfo.setTitle(m9.group(1));
                                zuInfo.setHouseOverview(m10.group(1));
                                zuInfo.setDecoration(m11.group(1));
                                zuInfo.setCity(m12.group(2).replace("安居客",""));
                                zuInfo.setDistrict(m13.group(6));
                                zuInfo.setStreet(m13.group(8));
                                zuInfo.setPlot(m6.group(2));
                                zuInfo.setFullAddress( m12.group(2).replace("安居客","")+"-"+m13.group(6)+"-"+m13.group(8)+"-"+ m6.group(2));
                                zuInfo.setImgUrls(imgSrcs);
                                zuInfo.setHouseConfig("");
                                zuInfo.setZuTime("");
                                zuInfo.setHouseStatus("待租");
                                zuInfo.setUserId(1L);
                                zuInfo.setCreateBy("admin");
                                zuInfo.setCreateTime(new Date());

                                saveHouseInfo(zuInfo);
                            }

                            tempReader.close();

                            tempInputStream.close();
                            tempConnection.disconnect();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            httpClient.close();
                            closeConnection();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                closeConnection();
            }
            pageStrat++;
        }
    }

    /**
     * 关闭close 连接
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private static void closeConnection() throws SQLException, ClassNotFoundException {
        Connection mysqlConnectionInstance = getMysqlConnectionInstance();
        mysqlConnectionInstance.close();
        connectionInstance = null;
    }

    /**
     * 保存爬取的数据至房源信息表
     * @param zuInfo 房源信息
     * @return
     */
    private static int saveHouseInfo(ZuInfo zuInfo) throws SQLException, ClassNotFoundException {
        Statement statement = getMysqlConnectionInstance().createStatement();
        String sql = createZuInfoInsertSql(zuInfo);
        System.out.println("sql = " + sql);
        return statement.executeUpdate(sql);
    }

    /**
     * 创建保存房源信息 sql
     * @param zuInfo
     * @return
     */
    private static String createZuInfoInsertSql(ZuInfo zuInfo) {

        Date createTime = zuInfo.getCreateTime();
        String createTimeStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, createTime);

        return "insert into house_zu_info (house_number, push_time, area, orientation, floor, price, house_type, title,\n" +
                "                           house_overview, decoration, city, district, street, plot, full_address, img_urls,\n" +
                "                           house_config, zu_time, house_status, user_id, create_by, create_time)\n" +
                "values ('"+zuInfo.getHouseNumber()+"','"+zuInfo.getPushTime()+"','"+zuInfo.getArea()+"','"+zuInfo.getOrientation()+"','"+
                zuInfo.getFloor()+"','"+zuInfo.getPrice()+"','"+zuInfo.getHouseType()+"','"+zuInfo.getTitle()+"','"+
                zuInfo.getHouseOverview()+"','"+zuInfo.getDecoration()+"','"+zuInfo.getCity()+"','"+zuInfo.getDistrict()+"','"+zuInfo.getStreet()+"','"+
                zuInfo.getPlot()+"','"+zuInfo.getFullAddress()+"','"+zuInfo.getImgUrls()+"','"+zuInfo.getHouseConfig()+"','"+zuInfo.getZuTime()+"','"+
                zuInfo.getHouseStatus()+"',"+zuInfo.getUserId()+",'"+zuInfo.getCreateBy()+"','"+createTimeStr+"');";
    }

    /**
     * 保存爬虫记录
     *
     * @return
     */
    private static int saveCrawlingRecord(CrawlingAnjukeRecord crawlingAnjukeRecord) throws SQLException, ClassNotFoundException {
        Statement statement = getMysqlConnectionInstance().createStatement();
        String sql = createCrawlingAnjukeRecordInsertSql(crawlingAnjukeRecord);
        System.out.println("sql = " + sql);
        return statement.executeUpdate(sql);

    }

    /**
     * 创建爬虫爬取记录sql
     * @param crawlingAnjukeRecord
     * @return
     */
    private static String createCrawlingAnjukeRecordInsertSql(CrawlingAnjukeRecord crawlingAnjukeRecord) {

        Date crawlingTime = crawlingAnjukeRecord.getCrawlingTime();
        String crawlingTimeStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,crawlingTime);

        return "insert into house_crawling_anjuke_record (use_cookie, use_userAgent, crawling_city, primary_page_url, page_num,\n" +
                "                                          crawling_time, create_by)\n" +
                "values ('"+crawlingAnjukeRecord.getUseCookie()+"','"+crawlingAnjukeRecord.getUseUseragent()+"','"+
                crawlingAnjukeRecord.getCrawlingCity()+"','"+crawlingAnjukeRecord.getPrimaryPageUrl()+"',"+
                crawlingAnjukeRecord.getPageNumber()+",'"+crawlingTimeStr+"','"+
                crawlingAnjukeRecord.getCreateBy()+"');";
    }

    /**
     * 获取mysql 连接
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    private static Connection getMysqlConnectionInstance() throws ClassNotFoundException, SQLException {

        if(StringUtils.isNull(connectionInstance)){
            Class.forName(driverName);
            connectionInstance = DriverManager.getConnection(mysqlUrl, username, password);
            return connectionInstance;
        }

        return connectionInstance;
    }
}